function openNew(id, imp) {
    imp = imp || 0;
    var op = $(this).data('opened') || false;	
    if (!op) {
			var data={};
		    $.ajax({
				//url:"/Public/Home/style/member/details.json",
                url:"/Home/Index/newsPage",
				type: 'get',
			    dataType: 'json',
		        data: {
		        	id: id
                },
				success:function(data){
	                if (data) {
	                    $('#news_' + id + ' .news_body').find('div').eq(0).remove();
	                    $('<div>' + replace_domain(data['content']) + '</div>').prependTo($('#news_' + id + ' .news_body'));
	                    $('#news_' + id + ' .news_body').slideDown();
	                    $.each($('.translate'),
	                    function(id, value) {
	                        $(value).html(getTranslation($(value).html()));
	                    });
	                    $('#news_' + id + ' .news_body .minim').append('<div style="display: inline-block; vertical-align: bottom; margin: 0 10px;"><div id="fb-root"></div>' + '<div class="fb-like" data-href="' + location.protocol + location.hostname + location.pathname + '#' + id + '" data-layout="button_count" data-action="like" data-show-faces="true" data-share="false"></div></div>'); (function(d, s, id) {
	                        var js, fjs = d.getElementsByTagName(s)[0];
	                        if (d.getElementById(id)) return;
	                        js = d.createElement(s);
	                        js.id = id;
	                        js.src = "";
	                        fjs.parentNode.insertBefore(js, fjs);
	                    } (document, 'script', 'facebook-jssdk'));
	                    if (imp) {
	                        var title = $('#news_' + id + ' span.date').next().text();
	                        $('<div id="win_new" iconCls="icon-save" zIndex="10000" title="' + getTranslation('Important new') + '!" style="padding: 20px; height: 500px; overflow-y: auto;">' + '<h2>' + title + '</h2>' + '<div class="news_body" style="display: block; font-size: 1.3em;">' + replace_domain(data['content']) + '</div>' + '</div>').appendTo('body');
	                        $('#win_new').window({
	                            inline: true,
	                            modal: true,
	                            width: 700,
	                            iconCls: 'icon-notify',
	                            onClose: function() {
	                                $('#win_new').window('destroy');
	                            }
	                        });
	                    }
	                }
				}
             });
        $(this).data('opened', true);
    } else {
        $(this).data('opened', false);
        $('#news_' + id + ' .news_body').slideUp();
    }
}
function paintNews(list) {
    var mt = $('#pattern_news_item').val();
    $('#news_list').html('');
    if (list) {
        for (var i in list) {
            var it = list[i];
            var n = format(mt, it.id, it.d_date, it.name);
            // console.log(n)
            $(n).appendTo($('#news_list'));
        }
    } else {
        $('#news_list').html(getTranslation('no_news'));
    }
}
function loadNews(s, o, id) {
    var imp = 0;
    if (id) {
        var ss = id.split('!');
        if (ss.length > 1) {
            id = ss[1];
            imp = 1;
        }
        id = goodParseInt(id);
    }
		
	var data;	
    $.ajax({
		//url:"/Public/Home/style/member/news.json",
        url:"/Home/Index/news",
		type: 'get',
	    dataType: 'json',
		success:function(result){
				data=result;
	           paintNews(data[0]);
               if (id) {
                   openNew(id, imp);
               }
               console.log(data);
               openNew(data[0][0]['id'], 1);
			}
		})
}
$(function() {
    $().window.defaults['zIndex'] = 1100000;
    id = '';
    if (window.location.hash) {
        var s = window.location.hash.split('#');
        if (typeof(s[1]) != 'undefined') {
            id = s[1];
        }
    }
    loadNews(0, 0, id);
    $('#add_new').die().live('click',
    function() {
        editNews(0);
        return false;
    });
    $('#news_list a[href^="!"]').die().live('click',
    function() {
        var s = $(this).attr('href').split('!');
        var id = goodParseInt(s[1]);
        openNew.call(this, id);
        return false;
    });
    $('a.news_delete').die().live('click',
    function() {
        deleteNews($(this).attr('data-id'));
        return false;
    });
    $('a.news_edit').die().live('click',
    function() {
        editNews($(this).attr('data-id'));
        return false;
    });
    $('a.minimize').die().live('click',
    function() {
        $(this).parent().parent().parent().find('a').eq(0).data('opened', false);
        $(this).parent().parent().slideUp();
    });
});